package com.ghc.service;

import com.ghc.service.MailService;

import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessagePreparator;

import com.ghc.vo.MailVo;

public class MailServiceImpl implements MailService{

	private JavaMailSender javaMailSender;
	private SimpleMailMessage sessage;

	@Override
	public void placeOrder(final MailVo mailVo) throws MailException {
		MimeMessagePreparator preparator = new MimeMessagePreparator(){

			@Override
			public void prepare(MimeMessage mimeMessage) throws Exception {
				// TODO Auto-generated method stub
				mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(mailVo.getEmail()));
                mimeMessage.setFrom(new InternetAddress(sessage.getFrom()));
                mimeMessage.setSubject(mailVo.getSubject(),"gbk");
                mimeMessage.setContent(mailVo.getContent(), "text/html;charset = gbk");
			}
        }; 
          javaMailSender.send(preparator);
	}

	public JavaMailSender getJavaMailSender() {
		return javaMailSender;
	}

	public void setJavaMailSender(JavaMailSender javaMailSender) {  
        this.javaMailSender = javaMailSender;  
    }

	public SimpleMailMessage getSessage() {
		return sessage;
	}

	public void setSessage(SimpleMailMessage sessage) {
		this.sessage = sessage;
	}
}
